/*!
 * vinculum
 * Copyright(c) 2017 azzly
 * MIT Licensed
 */
 
'use strict';

/**
 * Module dependencies.
 * @private
 */
const DB = require("./db");
const CONST = require("./CONST");
const debug = require("./debug")(__filename);

/**
 * Variables
 * @private
 */

let system_db;
let dbs = {};
 
/**
 * prototype.
 */

let obj = exports = module.exports = {};

/**
 * init
 *
 * @public
 */
 
obj.init = function(o){
  system_db = new DB(o);
};

/**
 * setDB
 *
 * @public
 */
 
obj.setDB = function(key,o){
  dbs[key] = new DB(o);
};

/**
 * getDB
 *
 * @public
 */
 
obj.getDB = function(key){
	if(!key || key === CONST.SYSTEMDBNAME){
		return system_db;
	}else{
		return dbs[key];
	}
};

/**
 * query
 *
 * @public
 */
 
obj.query = function(dbname,sql,data){
  return dbs[dbname].query(sql,data);
};

/**
 * systemQuery
 *
 * @public
 */
 
obj.systemQuery = function(sql,data){
  return system_db.query(sql,data);
};
